package com.atguigu.springcloud.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @auther zzyy
 * @create 2020-02-20 11:11
 */
@Service
public class PaymentService {
    /**
     * 正常访问，肯定OK
     *
     * @param id
     * @return
     */
    public String paymentInfo_OK(Integer id) {
        try {
            TimeUnit.MILLISECONDS.sleep(7000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "线程池:  " + Thread.currentThread().getName() + "  paymentInfo_OK,id:  " + id + "\t" + "O(∩_∩)O哈哈~";
    }

    /**
     * HystrixCommandProperties  HystrixProperty中的name值都在HystrixCommandProperties类中
     *
     * fallbackMethod  指定服务降级的处理方法
     * commandProperties 配置服务降级的相关参数
     *      execution.isolation.thread.timeoutInMilliseconds   设置超时的时间为3秒钟,超过3秒就服务降级
     */
    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    })
    public String paymentInfo_TimeOut(Integer id) {
        // 异常和时间超时都会服务降级
        //int age = 10/0;
        try {
            TimeUnit.MILLISECONDS.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "线程池:  323" + Thread.currentThread().getName() + " id:  " + id + "\t" + "O(∩_∩)O哈哈~" + "  耗时(秒): ";
    }

    public String paymentInfo_TimeOutHandler(Integer id) {
        return "线程池:  " + Thread.currentThread().getName() + "  8001系统繁忙或者运行报错，请稍后再试,id:  " + id + "\t" + "o(╥﹏╥)o";
    }

    /**
     *     HystrixCommand 一些配置信息
     *     @HystrixCommand(
     *             commandProperties ={
     *                     //请求超时时间
     *                     @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value ="3000" ),
     *                     //统计窗口内的定义
     *                     @HystrixProperty(name =
     *                             "metrics.rollingStats.timeInMilliseconds",value = "8000"),
     *                     //最小请求数
     *                     @HystrixProperty(name =
     *                             "circuitBreaker.requestVolumeThreshold",value = "2"),
     *                     //错误的请求比例
     *                     @HystrixProperty(name =
     *                             "circuitBreaker.errorThresholdPercentage",value = "50"),
     *                     //自我修复的活窗口时长
     *                     @HystrixProperty(name =
     *                             "circuitBreaker.sleepWindowInMilliseconds",value = "3000")
     *             }
     *             ,fallbackMethod ="myFallBack" //回退方法
     *             //线程池标识，要标识为唯一
     *             ,threadPoolKey = "findRibbonResumeOpenStateTimeout"
     *             //线程池属性
     *             ,threadPoolProperties = {
     *                     //核心纯种
     *                     @HystrixProperty(name = "coreSize",value ="1" ),
     *             //最大等待队列长度
     *             @HystrixProperty(name = "maxQueueSize",value ="20" )
     *     }
     *     )
     */

    /**
     * Hystrix舱壁模式（线程池隔离策略）
     *
     * @HystrixCommand
     * 如果不进⾏任何设置，所有熔断⽅法使⽤⼀个Hystrix线程池（10个线程），那么这样的话会导致问
     * 题，这个问题并不是扇出链路微服务不可⽤导致的，⽽是我们的线程机制导致的，如果⽅法A的请求把
     * 10个线程都⽤了，⽅法2请求处理的时候压根都没法去访问B，因为没有线程可⽤，并不是B服务不可
     * ⽤。
     *
     *     @HystrixCommand(
     *             commandProperties ={
     *                     @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value ="3000" )
     *             }
     *             ,fallbackMethod ="myFallBack" //回退方法
     *             //线程池标识，要标识为唯一
     *             ,threadPoolKey = "findRibbonResumeOpenStateTimeout"
     *             //线程池属性
     *             ,threadPoolProperties = {
     *                     //核心纯种
     *                     @HystrixProperty(name = "coreSize",value ="1" ),
     *             //最大等待队列长度
     *             @HystrixProperty(name = "maxQueueSize",value ="20" )
     *     }
     *     )
     *
     *
     */

}
